//===============================
//  功能 ：备注 
//  作者 ：上中野辅亚瑟王 
//  创建时间 ：2024-08-13  16-26-06 
//  Unity版本  ：2019.4.35f1 
//  变更时间 :  2024-08-13  16-26-06 
//===============================



using System.Collections.Generic;
using UnityEngine;

namespace ZS.U3D.BASE.Cast
{
    public static class CastManager
    {

        private static bool _isInitialize = false;
        private static GameObject _driver = null;
        public static GameObject Driver { get { return _driver; } }
        private static MonoBehaviour _behaviour;

        private static Queue<ICast> _castQueue = new Queue<ICast>();



        /// <summary>
        /// 初始化
        /// </summary>
        public static void Initialize()
        {
            if (_isInitialize)
            {
                MyDebug.LogWarning($"{nameof(CastManager)} is Initialized ");
                return;
            }
            {
                _isInitialize = true;
                _driver = new GameObject($"[{nameof(CastManager)}]");
                _behaviour = _driver.AddComponent<CastDriver>();
                UnityEngine.Object.DontDestroyOnLoad(_driver);
                MyDebug.Log($"{nameof(CastManager)} is initalize !");
            }
        }

        /// <summary>
        /// 只能在运行时调用
        /// </summary>
        /// <param name="cast"></param>
        /// <param name="duration"></param>
        public static void DrawCast(ICast cast, float duration = 0)
        {
#if UNITY_EDITOR
            cast.RemainingTime = duration;
            _castQueue.Enqueue(cast);
#endif
        }

        /// <summary>
        /// 只能在OnDrawGizmos 和 OnDrawGizmosSeletced  这两个方法中调用
        /// </summary>
        /// <param name="cast"></param>
        public static void OnDrawCast(ICast cast)
        {
#if UNITY_EDITOR
            cast.Draw();
#endif
        }

        internal static void OnDrawGizmos()
        {
#if UNITY_EDITOR
            int count = _castQueue.Count;
            for (int i = 0; i < count; i++)
            {
                ICast cast = _castQueue.Dequeue();
                cast.Draw();
                if (cast.RemainingTime > 0)
                {
                    cast.RemainingTime -= Time.deltaTime;
                    _castQueue.Enqueue(cast);
                }
            }
#endif
        }

    }
}
